#include "base_log.h"

#include <stdarg.h>
#include <stdio.h>

#include "base_time.h"


//-----------------------------------------------------------------------------
// Logging
//-----------------------------------------------------------------------------


static int _loglevel;

void log_printf(LogLevel lvl, const char *format, ...) // Log a formatted message. cree 4 niv de log systeme erreur
{
    const char *logsLevels[] =
    {
        "DBG ",
        "INFO",
        "WARN",
        "ERR ",
    } ;

    char buffer[4096];
    va_list args;
    va_start (args, format);

    // Log only if not cut
    if(lvl >= _loglevel)
    {
        vsprintf (buffer, format, args);
        fprintf(stderr, "[%s][%7.2f] ", logsLevels[lvl], time_get());
        fputs(buffer, stderr);
        fputc('\n', stderr);
    }
    va_end (args);
}

void log_puts(LogLevel lvl, const char *str) //Log a message erreur
{
    log_printf(lvl, str);
}

void log_set_level(LogLevel lvl) //Set logging level cutoff.
{
    _loglevel = lvl;
}

LogLevel log_get_level(void) //Get current logging level cutoff.
{
    return _loglevel;
}
